import {Component, OnInit} from "@angular/core";
import {Http} from "@angular/http";
import {LoginService} from "../service/login.service";
import {Router} from "@angular/router";
import {CommonLogin} from "../common/CommonLogin";
@Component({
  selector: 'forget-pas',
  templateUrl: './forget.html',
})
export class ForgetPaswordComponent extends CommonLogin implements  OnInit{
  public data:string;
  public random:string;
  constructor(public loginservice:LoginService,public router :Router){
    super();
  }
  ngOnInit(): void {
    this.generateValideCode();
  }

  private generateValideCode() {
    let oldRandom = this.random;
    this.random = Date.now().toString(36)
      +(Math.floor(Math.random() * 100000000) + 100000000).toString();
    let randomRequest = this.userIp + "/servlet/validateCodeServlet?" + Math.random() + "&random=" + this.random+"&oldRandom="+oldRandom;
    document.getElementById("codes").setAttribute("src", randomRequest);
  }
  public changeValideCode(){
    this.generateValideCode();
  }



  public Forgetnext(){
    var forgetEmail=(<HTMLInputElement>document.getElementById("forgetEmail")).value;
    var nextcode=  (<HTMLInputElement>document.getElementById("input1")).value;
    if(forgetEmail==null||forgetEmail==''){
      $.jBox.tip("请输入邮箱！");
      return;
    }
    if(nextcode==null||nextcode==''){
      $.jBox.tip("请输入验证码！");
      return;
    }

    if(this.IsEmail(forgetEmail)){
      sessionStorage.setItem("forgetEmail",forgetEmail);
      this.valideCode(nextcode);
    } else {
      $.jBox.tip("请输入正确的邮箱！");
    }
  }
  public valideCode(nextcode:string){
    this.loginservice.VerificationCode(nextcode,this.random).then(
      commonMsg=>{
        if(commonMsg==true){
          this.router.navigate(['/forget-next']);
        }else{
          $.jBox.tip("请输入正确的验证码");
        }
      });
  }

}
